Hi! Welcome to where my work is :) You can scroll through chronologically, or skip to particular sections you are interested in from the navigation bar on the left - please give it a while to load then mouse-over it.

1. Loading and joining data

Set working directory first

Loading required libraries

library(sf)
library(rgdal)
library(raster)
library(mapview)
library(spatstat)
library(sp)
library(rgeos)
library(maptools)
library(GISTools)
library(tmap)
library(sf)
library(geojson)
library(geojsonio)
library(tmaptools)

Opening shapefile, taken from ukdataservice.ac.uk, giving a map of Greater Manchester by 2011 English Census Merged Wards (hence data for basemap shapefile is by ward level).

require(rgdal)
require(ggplot2)
shp <- readOGR(dsn ="BoundaryData", layer="england_cmwd_2011", stringsAsFactors = F)
## OGR data source with driver: ESRI Shapefile 
## Source: "C:\Users\Tommy\Desktop\GIS_proj\Github\CASA0005GISProj\BoundaryData", layer: "england_cmwd_2011"
## with 215 features
## It has 4 fields
plot(shp)

#checking Coordinate Reference System of the shapefile, which was originally NA
summary(shp)
## Object of class SpatialPolygonsDataFrame
## Coordinates:
##        min      max
## x 351662.6 406087.2
## y 381165.4 421037.7
## Is projected: TRUE 
## proj4string :
## [+proj=tmerc +lat_0=49 +lon_0=-2 +k=0.9996012717 +x_0=400000
## +y_0=-100000 +datum=OSGB36 +units=m +no_defs +ellps=airy
## +towgs84=446.448,-125.157,542.060,0.1502,0.2470,0.8421,-20.4894]
## Data attributes:
##     label             altname              name          
##  Length:215         Length:215         Length:215        
##  Class :character   Class :character   Class :character  
##  Mode  :character   Mode  :character   Mode  :character  
##      code          
##  Length:215        
##  Class :character  
##  Mode  :character
crs(shp)
## CRS arguments:
##  +proj=tmerc +lat_0=49 +lon_0=-2 +k=0.9996012717 +x_0=400000
## +y_0=-100000 +datum=OSGB36 +units=m +no_defs +ellps=airy
## +towgs84=446.448,-125.157,542.060,0.1502,0.2470,0.8421,-20.4894

Specifying CRS of the greater manchester area shapefile as EPSG 4326 for latitude/longiture

Manchester <- spTransform(shp, CRS("+init=epsg:4326"))
st_crs(Manchester)
## Coordinate Reference System:
##   EPSG: 4326 
##   proj4string: "+proj=longlat +datum=WGS84 +no_defs"

Plot Manchester basemap

plot(Manchester)

Extract just the city of Manchester itself rather than greater Manchester, as the area would otherwise be too big for analysis and Ripley’s K.

#extract the city
cityManchester <- subset(Manchester, name %in% c("Didsbury West","Fallowfield","Gorton North","Gorton South","Harpurhey","Higher Blackley","Hulme","Levenshulme","Longsight","Miles Platting and Newton Heath","Moss Side","Moston","Northenden","Old Moat","Rusholme","Sharston","Whalley Range","Withington","Woodhouse Park","Ancoats and Clayton","Ardwick","Baguley","Bradford","Brooklands","Burnage","Charlestown","Cheetham","Chorlton","Chorlton Park","City Centre","Crumpsall","Didsbury East"))

#Check to see that the correct borough has been pulled out
tm_shape(cityManchester) +
  tm_polygons(col = NA, alpha = 0.5)

Read in police station data for greater manchester area, from the geoJSON file obtained using overpass-turbo.eu API and running a query for police stations in the Greater Manchester Area from OpenStreetMap.

Keeping only the points, as readOGR cannot read both points and polygons at the same time, and the points represent police stations.

pol <- rgdal::readOGR("C:/Users/Tommy/Desktop/GIS_proj/BoundaryData/polstn.geojson",require_geomType="wkbPoint")
## OGR data source with driver: GeoJSON 
## Source: "C:\Users\Tommy\Desktop\GIS_proj\BoundaryData\polstn.geojson", layer: "polstn"
## with 114 features;
## Selected wkbPoint feature type, with 51 rows
## It has 42 fields

Plotting the police station points

plot(pol)

summary(pol)
## Object of class SpatialPointsDataFrame
## Coordinates:
##                min       max
## coords.x1 -2.89431 -1.741055
## coords.x2 53.25742 53.778362
## Is projected: FALSE 
## proj4string :
## [+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0]
## Number of points: 51
## Data attributes:
##                id                  X.id      amenity     building 
##  node/1123287516: 1   node/1123287516: 1   police:51   office: 0  
##  node/1131472551: 1   node/1131472551: 1               yes   : 0  
##  node/1290032691: 1   node/1290032691: 1               NA's  :51  
##  node/1429962455: 1   node/1429962455: 1                          
##  node/1865698140: 1   node/1865698140: 1                          
##  node/233211757 : 1   node/233211757 : 1                          
##  (Other)        :45   (Other)        :45                          
##            type     addr.postcode           addr.street
##  multipolygon: 0   OL7 0BQ : 1    Grappenhall Road: 1  
##  NA's        :51   PR25 2EX: 1    Lancastergate   : 1  
##                    WA4 2AF : 1    Manchester Road : 1  
##                    WA6 7NW : 1    Ship Street     : 1  
##                    BL1 8UN : 0    Spendmore Lane  : 1  
##                    (Other) : 0    (Other)         : 1  
##                    NA's    :47    NA's            :45  
##           contact.phone                 contact.website
##  +44 161 872 5050: 0    http://www.gmp.police.uk: 0    
##  NA's            :51    NA's                    :51    
##                                                        
##                                                        
##                                                        
##                                                        
##                                                        
##                                name              opening_hours
##  'B' Divisional Headquarters     : 1   Mo-Sa 10:00-18:00: 0   
##  Adlington                       : 1   NA's             :51   
##  Altrincham Police Station       : 1                          
##  Ashton under lyne Police Station: 1                          
##  Bury Police Station             : 1                          
##  (Other)                         :19                          
##  NA's                            :27                          
##                 note    building.colour building.levels building.material
##  Layout appoximate: 0   wheat: 0        2   : 0         brick: 0         
##  NA's             :51   NA's :51        NA's:51         NA's :51         
##                                                                          
##                                                                          
##                                                                          
##                                                                          
##                                                                          
##   height   roof.colour roof.material roof.shape
##  4   : 0   grey: 0     gravel: 0     flat : 0  
##  NA's:51   NA's:51     NA's  :51     round: 0  
##                                      NA's :51  
##                                                
##                                                
##                                                
##                                                
##                     source                        addr.city 
##  OS_OpenData_StreetView: 2   Frodsham, Cheshire        : 1  
##  OS OpenData StreetView: 1   Huddersfield              : 1  
##  survey                : 1   Leyland                   : 1  
##  Bing                  : 0   Manchester                : 1  
##  Bing aerial image     : 0   Stockton Heath, Warrington: 1  
##  (Other)               : 0   (Other)                   : 0  
##  NA's                  :47   NA's                      :46  
##               phone      fhrs.id                        operator 
##  +44 161 856 5629: 0   335284: 0   Cheshire Constabulary    : 0  
##  +44 161 872 5050: 1   NA's  :51   Cheshire Police          : 2  
##  0845 458 6392   : 0               Greater Manchester Police: 1  
##  NA's            :50               Lancashire Constabulary  : 1  
##                                    NA's                     :47  
##                                                                  
##                                                                  
##  wheelchair                                addr.housename source.building
##  yes : 1    Atherton Police Station               : 0     Bing: 0        
##  NA's:50    Cheshire Constabulary - Police Station: 1     NA's:51        
##             Police Headquarters                   : 0                    
##             Stockton Heath Police Station         : 1                    
##             NA's                                  :49                    
##                                                                          
##                                                                          
##  source.geometry
##  Bing: 0        
##  NA's:51        
##                 
##                 
##                 
##                 
##                 
##                                                       designation
##  Stockport Divisional Headquarters Greater Manchester Police: 0  
##  NA's                                                       :51  
##                                                                  
##                                                                  
##                                                                  
##                                                                  
##                                                                  
##  roof.levels addr.housenumber
##  1   : 0     37  : 0         
##  NA's:51     NA's:51         
##                              
##                              
##                              
##                              
##                              
##                                                                                                 website  
##  http://www.gmp.police.uk                                                                           : 0  
##  https://www.cheshire.police.uk/contact-us/police-stations-and-custody/northwich-police-station.aspx: 0  
##  NA's                                                                                               :51  
##                                                                                                          
##                                                                                                          
##                                                                                                          
##                                                                                                          
##           created_by     is_in     listed_status
##  Potlatch 0.10b: 1   Fulwood: 1   Grade II: 2   
##  Potlatch 0.6a : 1   NA's   :50   NA's    :49   
##  NA's          :49                              
##                                                 
##                                                 
##                                                 
##                                                 
##                            wikimedia_commons      wheelchair.description
##  File:Police Station, Warrington.jpg: 1      No Longer in use: 1        
##  NA's                               :50      NA's            :50        
##                                                                         
##                                                                         
##                                                                         
##                                                                         
##                                                                         
##  entrance 
##  yes : 1  
##  NA's:50  
##           
##           
##           
##           
##           
##                                                                                                                                                                                         description
##  Opening Hours: Monday: 8am to 11pm Tuesday: 8am to 11pm Wednesday: 8am to 11pm Thursday: 8am to 11pm Friday: 8am to 11pm Saturday: 8am to 11pm Sunday: 8am to 11pm Bank Holidays: 8am to 11pm: 1  
##  NA's                                                                                                                                                                                         :50  
##                                                                                                                                                                                                    
##                                                                                                                                                                                                    
##                                                                                                                                                                                                    
##                                                                                                                                                                                                    
##                                                                                                                                                                                                    
##            ele      level        HE_ref  
##  Street Level: 1   0   : 1   1240198: 1  
##  NA's        :50   NA's:50   NA's   :50  
##                                          
##                                          
##                                          
##                                          
## 

Checking the CRS of the police station points - it is already EPSG4326!

st_crs(pol)
## Coordinate Reference System:
##   EPSG: 4326 
##   proj4string: "+proj=longlat +datum=WGS84 +no_defs"

Removing duplicates from police station points

polstns <- remove.duplicates(pol)

Plotting the police station points on top of the greater manchester area map using mapview

mapview::mapview(Manchester) + mapview::mapview(polstns, color = "white", col.regions = "black",legend=FALSE)

Basemap of Manchester is a SpatialPolygonsDataFrame Police station points is a SpatialPointsDataFrame

class(cityManchester)
## [1] "SpatialPolygonsDataFrame"
## attr(,"package")
## [1] "sp"
class(polstns)
## [1] "SpatialPointsDataFrame"
## attr(,"package")
## [1] "sp"

Plot the police stations in the Greater Manchester area using tmap

tmap_mode("view")
tm_shape(Manchester) +
  tm_polygons(col = NA, alpha = 0.5) +
tm_shape(polstns) +
  tm_dots(col = "blue")

Plot the police stations in the Manchester city area using tmap

tmap_mode("view")
tm_shape(cityManchester) +
  tm_polygons(col = NA, alpha = 0.5) +
tm_shape(polstns) +
  tm_dots(col = "blue")

Filtering for police stations which are within the Manchester city boundary

proj4string(cityManchester) <- CRS("+init=epsg:4326")
proj4string(polstns) <- CRS("+init=epsg:4326")

polsub <- polstns[cityManchester,]
#checking to see that they've been removed
tmap_mode("view")
tm_shape(cityManchester) +
  tm_polygons(col = NA, alpha = 0.5) +
tm_shape(polsub) +
  tm_dots(col = "blue")

Reading in the dataset for population density

library(readr)
popdens <- read_csv("popdens.csv")
summary(popdens)
##   geography            popdens      
##  Length:32          Min.   : 12.00  
##  Class :character   1st Qu.: 37.40  
##  Mode  :character   Median : 47.95  
##                     Mean   : 52.83  
##                     3rd Qu.: 66.53  
##                     Max.   :116.70

Joining first dataset of population density with the Manchester basemap

require(sp)
?sp::merge
manchester_popdens <- merge(cityManchester, popdens, by.x = "name", by.y = "geography",duplicateGeoms = TRUE)

Plotting map to test if join has succeeded

tmap_mode("view")
tm_shape(manchester_popdens) +
  tm_polygons(col = NA, alpha = 0.5) +
tm_shape(polsub) +
  tm_dots(col = "blue")

Reading in the dataset for deprivation - the average number (out of 4 - employment, education, health and disability and household overcrowding) of dimensions a household was deprived in was used

depr <- read_csv("deprivation.csv")
summary(depr)
##   geography          deprivation    
##  Length:32          Min.   :0.5989  
##  Class :character   1st Qu.:0.9484  
##  Mode  :character   Median :1.1398  
##                     Mean   :1.0764  
##                     3rd Qu.:1.2323  
##                     Max.   :1.4070

Joining second dataset of deprivation with the Manchester basemap

manchester_2 <- merge(manchester_popdens, depr, by.x = "name", by.y = "geography",duplicateGeoms = TRUE)

Plotting map to check if second join has succeeded

tmap_mode("view")
tm_shape(manchester_2) +
  tm_polygons(col = NA, alpha = 0.5) +
tm_shape(polsub) +
  tm_dots(col = "blue")

Reading in the dataset for SES - the average social grade by ward was taken, by using a numeric scale of AB = 1, C1 = 2, C2 = 3, DE = 4, so lower numbers would mean a higher social grade.

ses <- read_csv("SES.csv")
summary(ses)
##   geography          socialgrade   
##  Length:32          Min.   :1.739  
##  Class :character   1st Qu.:2.404  
##  Mode  :character   Median :2.744  
##                     Mean   :2.620  
##                     3rd Qu.:2.896  
##                     Max.   :3.119

Joining third dataset of SES with the Manchester basemap

manchester_merged <- merge(manchester_2, ses, by.x = "name", by.y = "geography",duplicateGeoms = TRUE)

Plotting map to check if third join has succeeded

tmap_mode("view")
tm_shape(manchester_merged) +
  tm_polygons(col = NA, alpha = 0.5) +
tm_shape(polsub) +
  tm_dots(col = "blue")

manchester_merged is the map of Manchester city with merged data on population density, social grade and SES.

2. Plotting/performing KDE for crime data in Manchester city

Next step would be to map the crime datapoints onto the Manchester basemap. Crime data for June 2019 for the Greater Manchester area was used. This was taken from https://data.police.uk/data/

crime <- read_csv("manchester_crime.csv")
crime_locations <- st_as_sf(crime, coords = c("Longitude","Latitude"), crs = 4326)
class(crime)
## [1] "spec_tbl_df" "tbl_df"      "tbl"         "data.frame"
require(sf)
crime_sf <- st_as_sf(x = crime, 
                        coords = c("Longitude", "Latitude"),
                        crs = "+init=epsg:4326")
# simple plot
plot(crime_sf)

crime_spdf <- as(crime_sf, "Spatial")


manchester_crime_test<-ggplot(crime, aes(x=Longitude,y=Latitude))+geom_point()+coord_equal()
manchester_crime_test

Plotting a 2D KDE of crime occurences in Manchester using the longitude and latitude data in the crime dataset

manchester_crime_test<-ggplot(crime, aes(x=Longitude,y=Latitude))+stat_bin2d(bins=30)
manchester_crime_test

Plotting a continuous distribution instead

manchester_crime_test+stat_density2d(aes(fill = ..level..), geom="polygon")

Plotting the crime datapoints to test

plot(crime_locations)

Examining metadata of the crime datapoints

summary(crime_locations)
##           geometry   
##  POINT        :8828  
##  epsg:4326    :   0  
##  +proj=long...:   0

Checking CRS of crime datapoints

crs(crime_locations)
## [1] "+proj=longlat +datum=WGS84 +no_defs"

Checking type of data of the crimem locations - sf, tbl_df, tbl, data.frame

class(crime_locations)
## [1] "sf"         "tbl_df"     "tbl"        "data.frame"

Ensuring that crime_locations has EPSG 4326 as CRS

crime_locations_sp <- as(crime_locations, 'Spatial')
crime_locations_sp <- spTransform(crime_locations_sp, CRS("+init=epsg:4326"))

Checking new CRS of crime datapoints and data type, removing duplicates

crs(crime_locations)
## [1] "+proj=longlat +datum=WGS84 +no_defs"
class(crime_locations)
## [1] "sf"         "tbl_df"     "tbl"        "data.frame"

Plotting crime data points on Manchester basemap

mapview::mapview(manchester_merged) + mapview::mapview(crime_locations, color = "white", col.regions = "black",legend=FALSE, title = "Crime occurences in Manchester city")

3. Descriptive statistics for data

library(tidyverse)
library(downloader)
library(rgdal)
library(sf)
library(ggplot2)
library(reshape2)
library(plotly)
library(highcharter)

Histograms for each of the scale variables - population densities, deprivation, and SES Starting with popdens

# Histogram with mean line (red) and median line (blue) and density plot
ggplot(popdens, aes(x=popdens)) + 
 geom_histogram(aes(y=..density..), colour="black", fill="white", binwidth=10)+
 geom_density(alpha=.2, fill="#FF6666") + geom_vline(aes(xintercept=mean(popdens)),
            color="red", linetype="dashed", size=1) + geom_vline(aes(xintercept=median(popdens)),
            color="blue", linetype="dashed", size=1) 

# Descriptive statistics
print(max(popdens$popdens))
## [1] 116.7
print(min(popdens$popdens))
## [1] 12
print(median(popdens$popdens))
## [1] 47.95
print(sd(popdens$popdens))
## [1] 22.6814
print(IQR(popdens$popdens))
## [1] 29.125
print(quantile(popdens$popdens, c(.25, .75)))
##    25%    75% 
## 37.400 66.525

Plotting histogram for deprivation next

# Histogram with mean line (red) and median line (blue) and density plot
ggplot(depr, aes(x=deprivation)) + 
 geom_histogram(aes(y=..density..), colour="black", fill="white", binwidth= 0.1)+
 geom_density(alpha=.2, fill="#FF6666") + geom_vline(aes(xintercept=mean(deprivation)),
            color="red", linetype="dashed", size=1) + geom_vline(aes(xintercept=median(deprivation)),
            color="blue", linetype="dashed", size=1) 

# Descriptive statistics
print(max(depr$deprivation))
## [1] 1.406953
print(min(depr$deprivation))
## [1] 0.5988858
print(median(depr$deprivation))
## [1] 1.139765
print(sd(depr$deprivation))
## [1] 0.2171107
print(IQR(depr$deprivation))
## [1] 0.283867
print(quantile(depr$deprivation, c(.25, .75)))
##       25%       75% 
## 0.9484226 1.2322897

Plotting histogram for social grade next

# Histogram with mean line (red) and median line (blue) and density plot
ggplot(ses, aes(x=socialgrade)) + 
 geom_histogram(aes(y=..density..), colour="black", fill="white", binwidth= 0.1)+
 geom_density(alpha=.2, fill="#FF6666") + geom_vline(aes(xintercept=mean(socialgrade)),
            color="red", linetype="dashed", size=1) + geom_vline(aes(xintercept=median(socialgrade)),
            color="blue", linetype="dashed", size=1) 

# Descriptive statistics
print(max(ses$socialgrade))
## [1] 3.118662
print(min(ses$socialgrade))
## [1] 1.738781
print(median(ses$socialgrade))
## [1] 2.744045
print(sd(ses$socialgrade))
## [1] 0.3916912
print(IQR(ses$socialgrade))
## [1] 0.4920259
print(quantile(ses$socialgrade, c(.25, .75)))
##      25%      75% 
## 2.403788 2.895813

Plotting histogram for crimes recorded per ward next

#reading in data for crimes recorded per ward (taken from later part of the code after performing count)
library(readr)
crime_ward <- read_csv("crime_ward.csv")
summary(crime_ward)
##   geography             crimes      
##  Length:32          Min.   :  96.0  
##  Class :character   1st Qu.: 145.2  
##  Mode  :character   Median : 183.0  
##                     Mean   : 275.7  
##                     3rd Qu.: 330.8  
##                     Max.   :1887.0
crime_ward$lnCrime <- log1p(crime_ward$crimes)
# Histogram with mean line (red) and median line (blue) and density plot
ggplot(crime_ward, aes(x=lnCrime)) + 
 geom_histogram(aes(y=..density..), colour="black", fill="white", binwidth= 0.05)+
 geom_density(alpha=.2, fill="#FF6666") + geom_vline(aes(xintercept=mean(lnCrime)),
            color="red", linetype="dashed", size=1) + geom_vline(aes(xintercept=median(lnCrime)),
            color="blue", linetype="dashed", size=1) 

# Descriptive statistics
print(max(crime_ward$crimes))
## [1] 1887
print(min(crime_ward$crimes))
## [1] 96
print(median(crime_ward$crimes))
## [1] 183
print(sd(crime_ward$crimes))
## [1] 313.0229
print(IQR(crime_ward$crimes))
## [1] 185.5
print(quantile(crime_ward$crimes, c(.25, .75)))
##    25%    75% 
## 145.25 330.75

Converting both cityManchester and crime_locations to data.frames to run the KDE, since it requires numeric input

df_cityManchester <- as.data.frame(cityManchester)
df_crime_locations <- as.data.frame(crime_locations)

Create two separate fields for latitude and longitude from the manchester spatialpolygonsdataframe

polys = attr(cityManchester,'polygons')
npolys = length(polys)
for (i in 1:npolys){
  poly = polys[[i]]
  polys2 = attr(poly,'Polygons')
  npolys2 = length(polys2)
  for (j in 1:npolys2){
     #do stuff with these values
     coords = coordinates(polys2[[j]])
     
  }
}
coords_df <- as.data.frame(coords)

Plotting out crime occurences in Manchestaer city

ggplot() + geom_polygon(data = coords_df, aes(x = V1, y = V2), fill = "grey75") +
  geom_point(data = crime, aes(x = Longitude, y = Latitude),
             col = "dodger blue", alpha = .5, size = 1.5) +
  coord_equal() +
  ggtitle("Crime occurences in Manchester")

Counting number of crime occurence points per polygon

class(manchester_merged)
## [1] "SpatialPolygonsDataFrame"
## attr(,"package")
## [1] "sp"
class(crime_spdf)
## [1] "SpatialPointsDataFrame"
## attr(,"package")
## [1] "sp"
proj4string(manchester_merged)
## [1] "+init=epsg:4326 +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0"
crime_spdf <- spTransform(crime_spdf, CRS("+init=epsg:4326"))
proj4string(crime_spdf)
## [1] "+init=epsg:4326 +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0"
plot(manchester_merged)
plot(crime_spdf, col="red" , add=TRUE)

res <- over(crime_spdf, manchester_merged)
table(res$name)
## 
##             Ancoats and Clayton                         Ardwick 
##                             484                             368 
##                         Baguley                        Bradford 
##                             179                             333 
##                      Brooklands                         Burnage 
##                             136                             149 
##                     Charlestown                        Cheetham 
##                             176                             425 
##                        Chorlton                   Chorlton Park 
##                             108                             137 
##                     City Centre                       Crumpsall 
##                            1887                             270 
##                   Didsbury East                   Didsbury West 
##                             101                             126 
##                     Fallowfield                    Gorton North 
##                             151                             298 
##                    Gorton South                       Harpurhey 
##                             212                             443 
##                 Higher Blackley                           Hulme 
##                             355                             213 
##                     Levenshulme                       Longsight 
##                             199                             187 
## Miles Platting and Newton Heath                       Moss Side 
##                             334                             219 
##                          Moston                      Northenden 
##                             148                             171 
##                        Old Moat                        Rusholme 
##                             136                             172 
##                        Sharston                   Whalley Range 
##                             151                             129 
##                      Withington                  Woodhouse Park 
##                              96                             330

After plotting number of crimes per ward on a .csv file, time to merge with the manchester_merged basemap. Reading in the dataset for crimes per ward using code chunk directly above

summary(crime_ward)
##   geography             crimes          lnCrime     
##  Length:32          Min.   :  96.0   Min.   :4.575  
##  Class :character   1st Qu.: 145.2   1st Qu.:4.985  
##  Mode  :character   Median : 183.0   Median :5.215  
##                     Mean   : 275.7   Mean   :5.380  
##                     3rd Qu.: 330.8   3rd Qu.:5.804  
##                     Max.   :1887.0   Max.   :7.543

Joining crimes per ward dataset with the Manchester basemap

manchester_merged_final <- merge(x= manchester_merged, y=crime_ward, by.x = "name", by.y = "geography")

Plotting map to check if join has succeeded.

library(tmap)
tmap_mode("view")
tm_shape(manchester_merged_final) +
  tm_polygons(col = NA, alpha = 0.5) +
tm_shape(polsub) +
  tm_dots(col = "blue")

All data (popdens, social grade, SES, crime) on ward level loaded, locations of 4 police stations in Manchester city also loaded.

4. Choropleth maps by variables across Manchester

Making a choropleth map by crimes

library(ggplot2)
library(RColorBrewer)
library(classInt)
names(manchester_merged_final)
## [1] "name"        "label"       "altname"     "code"        "popdens"    
## [6] "deprivation" "socialgrade" "crimes"      "lnCrime"
#spplot(manchester_merged_final, "crimes")
# quantile breaks
breaks_qt <- classIntervals(manchester_merged_final$crimes, n = 7, style = "quantile")
br <- breaks_qt$brks 
offs <- 0.0000001 
br[1] <- br[1] - offs 
br[length(br)] <- br[length(br)] + offs 
# categories for choropleth map
manchester_merged_final$crimes_bracket <- cut(manchester_merged_final$crimes, br)
# plot
#my.palette <- brewer.pal(n = 7, name = "OrRd")
#spplot(manchester_merged_final, "crimes_bracket", col.regions=my.palette, main = "Manchester City Recorded Crimes in June 2019 by Ward")

#Using tmap
library(tmap)
tm_shape(manchester_merged_final) +
  tm_polygons("crimes", 
              style="quantile", 
              title="Manchester city \nrecorded crimes \nby ward in June 2019")
tmap_mode("view")

Making a choropleth map by popdens

# quantile breaks
breaks_qt <- classIntervals(manchester_merged_final$popdens, n = 7, style = "quantile")
br <- breaks_qt$brks 
offs <- 0.0000001 
br[1] <- br[1] - offs 
br[length(br)] <- br[length(br)] + offs 

# categories for choropleth map
manchester_merged_final$popdens_bracket <- cut(manchester_merged_final$popdens, br)
# plot
#spplot(manchester_merged_final, "popdens_bracket", col.regions=my.palette, main = "Manchester City Population Density by Ward")

#Using tmap
library(tmap)
tm_shape(manchester_merged_final) +
  tm_polygons("popdens", 
              style="quantile", 
              title="Manchester city \nPopulation Density \nby ward")
tmap_mode("view")

Making a choropleth map by deprivation

#spplot(manchester_merged_final, "deprivation")
breaks_qt <- classIntervals(manchester_merged_final$deprivation, n = 7, style = "quantile")
br <- breaks_qt$brks 
offs <- 0.0000001 
br[1] <- br[1] - offs 
br[length(br)] <- br[length(br)] + offs 
# categories for choropleth map
manchester_merged_final$deprivation_bracket <- cut(manchester_merged_final$deprivation, br)
# plot

#spplot(manchester_merged_final, "deprivation_bracket", col.regions=my.palette, main = "Manchester City deprivation by Ward")
#Using tmap
library(tmap)
tm_shape(manchester_merged_final) +
  tm_polygons("deprivation", 
              style="quantile", 
              title="Manchester city \ndeprivation \nby ward")
tmap_mode("view")

Making a choropleth map by social grade

#spplot(manchester_merged_final, "socialgrade")
breaks_qt <- classIntervals(manchester_merged_final$socialgrade, n = 7, style = "quantile")
br <- breaks_qt$brks 
offs <- 0.0000001 
br[1] <- br[1] - offs 
br[length(br)] <- br[length(br)] + offs 
# categories for choropleth map
manchester_merged_final$socialgrade_bracket <- cut(manchester_merged_final$socialgrade, br)
# plot
#spplot(manchester_merged_final, "socialgrade_bracket", col.regions=my.palette, main = "Manchester City social grade by Ward")
#Using tmap
library(tmap)
tm_shape(manchester_merged_final) +
  tm_polygons("socialgrade", 
              style="quantile", 
              title="Manchester city \nsocial grade \nby ward")
tmap_mode("view")